package net.obviam.logic;

import java.util.ArrayList;
import java.util.Iterator;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.utils.Array;

import net.obviam.starassault.controller.WorldController;
import net.obviam.starassault.model.Block;
import net.obviam.starassault.model.Block.BlockType;
import net.obviam.starassault.model.Bob;
import net.obviam.starassault.model.Bullet;
import net.obviam.starassault.model.World;
import net.obviam.starassault.screens.GameScreen;


public class Simulation {
	public Bob bob;
	public World world;
	public GameScreen gs;
	public WorldController wc;
	public int col = 0;
	public Block activeBlock;
	public ArrayList<Bullet> bulletList;

	public OrthographicCamera gameCam, gameUICam;

	public Simulation(GameScreen gs){
		gameCam = new OrthographicCamera();
		gameCam.zoom = (float)128f*10f/(float)Gdx.graphics.getWidth();
		gameCam.position.set(Gdx.graphics.getWidth()*gameCam.zoom/2, Gdx.graphics.getHeight()*gameCam.zoom/2, 0);
		gameUICam = new OrthographicCamera();
		gameUICam.zoom = 1f;
		gameUICam.position.set(Gdx.graphics.getWidth()*gameUICam.zoom/2, Gdx.graphics.getHeight()*gameUICam.zoom/2, 0);
		gameUICam.viewportWidth = Gdx.graphics.getWidth();
		gameUICam.viewportHeight = Gdx.graphics.getHeight();
		gameUICam.update();
		this.gs = gs;
		world = new World();
		wc = new WorldController(world);
		this.bob = world.getBob();
		bulletList = new ArrayList<Bullet>();
	}

	public void collisionDetection(){
		Array<Block> block = world.getBlocks();
		for(int i = 0; i < block.size; i++){
			//bob
			if(bob.getBounds().overlaps(block.get(i).getBounds()) && block.get(i) != activeBlock){
				activeBlock = block.get(i);
				if(block.get(i).isBlockType(BlockType.Obstacle)){
					bob.setScore(bob.getScore()-400);
				}
				if(block.get(i).isBlockType(BlockType.Wall)){
					bob.setScore(bob.getScore()-2000);
				}
				if(block.get(i).isBlockType(BlockType.points)){
					bob.setScore(bob.getScore()+100);
					block.removeIndex(i);
				}
			} 
			//bullets
			for(Iterator<Bullet> it = bulletList.iterator(); it.hasNext();){
				Bullet b = it.next();
				if(b.getBounds().overlaps(block.get(i).getBounds())){
					it.remove();
					block.get(i).decHP();
					if(block.get(i).getHP() <= 0){
						block.removeIndex(i);
					}

				}
			}
		}
	}
	
	public ArrayList<Bullet> getBulletList(){
		return bulletList;
	}
	  
	public void update(){
		if(!gs.gameOver){
			wc.update();
			bob.update(bulletList);
			if(bob.getScore() < 0){
				gs.gameOver = true;
				
			}
			bob.setBounds(bob.getPosition().x*gs.renderer.getppuY(), bob.getPosition().y*gs.renderer.getppuY(), bob.SIZE*gs.renderer.getppuY(), bob.SIZE*gs.renderer.getppuX());
			for(Bullet bullet : bulletList){
				bullet.update(gs.renderer.getppuX(), gs.renderer.getppuY());;
			}
			for(Iterator<Bullet> it = bulletList.iterator(); it.hasNext();){
				Bullet b = it.next();
				if(!b.getBulletLife()){
					it.remove();
				}
			}
			collisionDetection();
		}
 
		//System.out.println(Gdx.graphics.getFramesPerSecond());
		//System.out.println(bob.getState());
	}


}
